<?php
    $registry   = Zend_Registry::getInstance();
    $constants = $registry->constants;
    $this->headLink()->appendStylesheet($this->baseUrl().'/static/css/main.css');
    $this->headLink()->appendStylesheet($this->baseUrl().'/static/css/place.css');
    $this->headScript()->appendFile("/static/js/jquery.js");
    $this->headScript()->appendFile("/static/js/jquery-ui-1.10.3.custom.js");
    $this->headScript()->appendFile("/static/js/jquery-ui-1.10.3.custom.min.js");
    $this->headScript()->appendFile("/static/js/jquery-ui-1.10.3.custom.min.js");
?>
<html>
    <head>
        <?php 
            echo $this->headLink();
            echo $this->headScript();
            ?>
        <meta charset="utf-8">
        <title>Edit Place</title>
        <link rel="shortcut icon" href="/static/images/favicon.png">
        
        
    </head>
    <body>
        <div class="wrapper">
            <div class="header">
                <div class="topbar-center">
                    <span style="color:#DF5A49;font-weight: bold">EDIT</span>
                    <span style="color:#000">PLACE</span>
                </div>
                <div class="login-logout">
                    <span><?php echo "Xin chào ".$this->userName?></span> | <span><a href="/place/index/logout">LogOut</a></span>

                </div>
            </div>
            <div class="content">
                <div class="menu-left">
                    <ul>
                    <?php foreach($this->userFeature as $key=>$value){?>
                        <li><a href="<?php echo $value->feature_url?>"><?php echo $value->feature_name;?></a></li>
                    <?php }?>
                    </ul>
                </div>
                <div class="edit-place-content">
                    <div class="content1-title" >
                    </div>
                    <div class="alert-title" id="alert"></div>
                        <div class="edit-place-input-left">
                            <form enctype="multipart/form-data" id="myform">
                                <div class="alert-place-add" ></div>
                                <input type="hidden" name="latitude" id="latitude" value="<?php echo $this->place->latitude; ?>" />
                                <input type="hidden" name="longitude" id="longitude" value="<?php echo $this->place->longitude; ?>" />
                                <input type="hidden" name="place_id" id="place_id" value="<?php echo $this->place->place_id; ?>" />
                                <div class="input-field-left">Tên: </div>
                                <input class="input-field-right" name="name" id="name" maxlength="255" value="<?php echo $this->place->name; ?>" m/>
                                <div class="input-field-left">Địa chỉ:</div>
                                <input class="input-field-right" name="address" id="address" maxlength="1000" value="<?php echo $this->place->address; ?>" />
                                <div class="input-field-left">Điện thoại:</div>
                                <input class="input-field-right" name="phone" maxlength="45" value= "<?php echo $this->place->phone; ?>"/>
                                <div class="input-field-left">Email:</div>
                                <input class="input-field-right" name="mail" maxlength="255" value="<?php echo $this->place->mail; ?>"/>
                                <div class="input-field-left">Website:</div>
                                <input class="input-field-right" name="website" maxlength="100" value="<?php echo $this->place->website; ?>"/>
                                <div style="width:100%;display:block; float: left">
                                   <div class="input-field-left">Mô tả:</div>
                                   <textarea class="input-field-right-area2" maxlength="600" name="description" ><?php echo $this->place->description;?></textarea>
                                </div>
                                <div style="width:100%;display:block; float: left">
                                    <div class="input-field-left">Sản phẩm, dịch vụ:</div>
                                    <textarea class="input-field-right-area2" name="product_service" maxlength="1000" id="product_service"><?php echo $this->place->product_service; ?></textarea>
                                    <button type="button" onclick="createTag('/place/merchant-place/get-tag')">Tạo tag</button>
                                </div>
                                <div class="input-field-left">Tag Category:</div>
                                 <select class="input-field-right" id="tag_category" name="tag_category">
                                    <?php foreach ($this->tagCategory as $key => $value) {?>
                                        <option value="<?php echo $value;?>" ><?php echo $value;?></option>
                                    <?php }?>
                                 </select>
                                <div style="width:100%;display:block; float: left">
                                   <div class="input-field-left">Tag:</div>
                                   <textarea class="input-field-right-area2" name="tag" id="tag" maxlength="1000" ><?php echo $this->place->tag;?></textarea>
                                </div>
                            </form>
                         </div>
                         
                         <div class="edit-place-input-right">
                             <div class="alert-place-add" ></div>
                             <div id="map_canvas" class="map-canvas">
                             </div>
                         </div>
                    
                    <div class="button-add">
                        <button class="button-test" onclick="updatePlace('/place/place-edit/edit')">
                            Update
                        </button>
                        <button class="button-test" onclick="history.back();">
                            Back
                        </button>
                    </div>
                </div>
                <div class="deal-content2">
                    <div class="table-alt" id="table-place-comment">
                    </div>
                </div>
            </div>
            
        </div>

    </body>
</html>
<script>

    $(document).ready(function(){
        $("#name").focus();
    });
    
    function updatePlace(url){
        place_name = $.trim($("#name").val());
        place_address = $.trim($("#address").val());
        product_service = $.trim($("#product_service").val());
        latitude = $.trim($("#latitude").val());
        longitude = $.trim($("#longitude").val());
        place_id = $.trim($("#place_id").val());
        
        if(place_name == '' ||
            place_address == '' ||
            product_service == '' ||
            latitude == '' ||
            longitude == '' ||
            place_id == ''){
            
            if(place_name == ''){
                $("#name").focus();
                $("#name").select();    
            }
            
            if(product_service == ''){
                $("#product_service").focus();
                $("#product_service").select();
            }
            
            if(place_address == ''){
                $("#address").focus();
                $("#address").select();
            }
            
            $("#alert").text('<?php echo $constants->MISSING_PARAMETER;?>');
            
        }else{
            var form = new FormData($('#myform')[0]);
            $.ajax({
                url : url,
                type : 'POST',
                data : form,
                success : function(res) {
                    if(res == "<?php echo $constants->UPDATE_MERCHANT_SUCCESS?>"){
                        
                    }else{
                        $("#name").focus();
                        $("#name").select();
                    }
                    $('#alert').html(res);
                },
                cache : false,
                contentType : false,
                processData : false
            });
        }
    }
    
    
    function createTag(url){
        $name = $('#product_service').val();
        $title = $('#name').val();
        $category = $('#tag_category').val();
        $.get(url,{
            "name":$name,
            "title":$title,
            "category":$category
            },function(responseData){
                $('#tag').val(responseData);    
            }
        );
    }
    
</script>

<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
    function initialize() {
        var map_canvas = document.getElementById('map_canvas');
        var myLatlng = new google.maps.LatLng(<?php echo $this->place->latitude;?>, <?php echo $this->place->longitude;?>);
        var map_options = {
          center: myLatlng,
          zoom: 16,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        
        var map = new google.maps.Map(map_canvas, map_options);
        
        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            draggable:true,
            title: 'Hello World!'
        });
        
        google.maps.event.addListener(marker, 'dragend', function(a){
            document.getElementById("latitude").value = this.getPosition().lat();
            document.getElementById("longitude").value = this.getPosition().lng();
        });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    
   
</script>